<template>
  <div id="windowEasyManage">
    <el-form :model="form_function_choice" label-width="80px">
      <el-form-item label="同步">
        <el-space wrap spacer="|">
          <!-- <el-button type="success" size="small" plain @click="session_setting_write">保存</el-button>-->
          <el-button type="warning" size="small" plain @click="session_setting_read">读取</el-button>
        </el-space>
      </el-form-item>
      <el-form-item label="日志标题">
        <el-input v-model="form_function_choice.username" @input="get_changelog_title"
                  placeholder="输入姓名"></el-input>
        <el-input v-model="form_function_choice.changelog_title" placeholder="日志标题" style="margin-top: 5px">
          <template #append>
            <el-button @click="do_copy(form_function_choice.changelog_title)" type="success">
              <el-icon>
                <DocumentCopy/>
              </el-icon>
            </el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="单包源">
        <el-input
            v-model="form_function_choice.link_list_dan_bao"
            :autosize="{ minRows: 2, maxRows: 20 }"
            type="textarea"
            @input="link_list_dan_bao_parsed"
            placeholder="输入koji地址，生成ID脚本"
        />
        <el-input
            style="margin-top: 5px"
            v-model="form_function_choice.link_list_dan_bao_parsed"
            :autosize="{ minRows: 2, maxRows: 20 }"
            type="textarea"
            placeholder="显示生成结果"
        />
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import axios from 'axios'
import {ElNotification} from 'element-plus'
import {DocumentCopy} from '@element-plus/icons-vue'
import getPara from '@/utils/get_para_by_url'

export default {
  name: "windowEasyManage",
  props: {
    sessionToken: String
  },
  components: {
    DocumentCopy,
  },
  data() {
    return {
      form_function_choice: {
        username: "",
        changelog_title: "",
        link_list_dan_bao: "http://10.1.123.237/koji/taskinfo?taskID=113198\n" +
            "http://10.1.123.238/koji/taskinfo?taskID=387201\n" +
            "http://10.1.123.239/koji/taskinfo?taskID=110184",
        link_list_dan_bao_parsed: ""
      },
    }
  },
  methods: {
    get_changelog_title() {
      // 星期几 月 日 年
      let time_str = new Date().toDateString()
      this.form_function_choice.changelog_title = '* ' + time_str + " " + this.form_function_choice.username + " <" +
          this.form_function_choice.username + "@kylinos.cn> "
    },
    do_copy(copy_str) {
      this.$copyText(copy_str).then(function () {
        ElNotification({
          title: '复制成功',
          type: 'success',
        })
      }, function () {
        ElNotification({
          title: '复制失败',
          type: 'error',
        })
      })
    },
    session_setting_write() {
      let obj_this = this
      let data = {
        "session_token": obj_this.sessionToken,
        "session_function": "session_setting_write",
        "session_setting": obj_this.form_function_choice
      }
      axios.post('/session_function_do', data).then(function (response) {
        ElNotification({
          title: response.data.msg_str,
          type: 'success',
        })
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
    },
    session_setting_read() {
      let obj_this = this
      let data = {
        "session_token": obj_this.sessionToken,
        "session_function": "session_setting_read"
      }
      axios.post('/session_function_do', data).then(function (response) {
        ElNotification({
          title: response.data.msg_str,
          type: 'success',
        })
        Object.assign(obj_this.form_function_choice, response.data.data)
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
    },
    link_list_dan_bao_parsed() {
      this.form_function_choice.link_list_dan_bao_parsed = ""
      let link_list_dan_bao_list = this.form_function_choice.link_list_dan_bao.split('\n')
      for (let i = 0; i < link_list_dan_bao_list.length; i++) {
        if (link_list_dan_bao_list[i] === "" || link_list_dan_bao_list[i].startsWith('#')) {
          this.form_function_choice.link_list_dan_bao_parsed += link_list_dan_bao_list[i] + '\n'
          continue
        }
        let url_parsed = getPara.parse_koji_url(link_list_dan_bao_list[i])
        if (url_parsed.length !== 4) {
          this.form_function_choice.link_list_dan_bao_parsed = "第" + i + "行：" + link_list_dan_bao_list[i] + "解析出错"
          break
        }
        let link_parsed_one = url_parsed[2] + " " + url_parsed[3] + " "
        switch (url_parsed[2]) {
          case "237":
            link_parsed_one += "V10SP1";
            break
          case "238":
            link_parsed_one = link_parsed_one + "V10SP1" + "\n" + link_parsed_one + "V10SP2";
            break
          case "239":
            link_parsed_one += "V10SP1";
            break
        }
        this.form_function_choice.link_list_dan_bao_parsed += link_parsed_one + '\n'
      }

    }
  },
  created() {
    this.session_setting_read()
    this.link_list_dan_bao_parsed()
  },
}
</script>

<style scoped>
#windowEasyManage {
  padding: 10px;
}
</style>